<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon :icon-class="isFullScreen ? 'exit-fullscreen' : 'fullscreen'" class="fullscreen" @click="toggleScreen" />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false // false未开启 true已开启
    }
  },
  mounted() {
    // 监听esc
    document.addEventListener('fullscreenchange', e => {
      // 判断退出全屏的条件
      const isFull = document.fullscreenElement
      // isFull在退出全屏的时候 是null
      if (isFull == null) {
        console.log('退出全屏了')
        this.isFullScreen = false
      }
    })
  },
  methods: {
    toggleScreen() {
      if (this.isFullScreen) {
        // 关闭全屏
        document.exitFullscreen()
      } else {
        //  开启全屏
        document.documentElement.requestFullscreen()
      }
      this.isFullScreen = !this.isFullScreen
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 18px;
  height: 18px;
  color: #fff;
  cursor: pointer;
}
</style>
